package com.example.hqpatrol.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.hqpatrol.R;
import com.example.hqpatrol.base.BaseFragment;
import com.example.hqpatrol.databinding.FragmentPatrolHomeBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem;
import com.xuexiang.xui.widget.banner.widget.banner.SimpleImageBanner;
import com.xuexiang.xutil.data.ConvertTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Page
public class PatrolHomeFragment extends BaseFragment<FragmentPatrolHomeBinding> {

    private ViewPager viewPager;

    @NonNull
    @Override
    protected FragmentPatrolHomeBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentPatrolHomeBinding.inflate(inflater, container, false);
    }

    @Override
    protected TitleBar initTitle() {
        return null;
    }

    @Override
    protected void initViews() {
        viewPager = getActivity().findViewById(R.id.include_main).findViewById(R.id.view_pager);

        List<BannerItem> bannerItemList = new ArrayList<>();
        bannerItemList.add(new BannerItem().setTitle("第一张图片").setImgUrl("https://picsum.photos/640/360?i=1"));
        bannerItemList.add(new BannerItem().setTitle("第二张图片").setImgUrl("https://picsum.photos/640/360?i=2"));
        bannerItemList.add(new BannerItem().setTitle("第三张图片").setImgUrl("https://picsum.photos/640/360?i=3"));
        bannerItemList.add(new BannerItem().setTitle("第四张图片").setImgUrl("https://picsum.photos/640/360?i=4"));
        SimpleImageBanner simpleImageBanner = findViewById(R.id.home_banner);
        simpleImageBanner.setSource(bannerItemList);
        simpleImageBanner.setIsOnePageLoop(false);
        simpleImageBanner.startScroll();

        binding.homeCardReady.cardTitle.setText(R.string.patrol_ready);
        binding.homeCardReady.cardIcon.setImageResource(R.drawable.ic_patrol_ready);
        binding.homeCardReady.cardText1.setText("当前待巡检数量");
        binding.homeCardReady.cardText2.setText("0");
        binding.homeCardReady.cardDetail.setOnClickListener(v -> {
            viewPager.setCurrentItem(1);
        });

        binding.homeCardFinished.cardTitle.setText(R.string.patrol_finished);
        binding.homeCardFinished.cardIcon.setImageResource(R.drawable.ic_patrol_finished);
        binding.homeCardFinished.cardText1.setText("当前已完成数量");
        binding.homeCardFinished.cardText2.setText(String.valueOf(new Random().nextInt(100)));
        binding.homeCardFinished.cardDetail.setOnClickListener(v -> {
            viewPager.setCurrentItem(2);
        });

        binding.homeCardTimeout.cardTitle.setText(R.string.patrol_timeout);
        binding.homeCardTimeout.cardIcon.setImageResource(R.drawable.ic_patrol_timeout);
        binding.homeCardTimeout.cardText1.setText("当前已超期数量");
        binding.homeCardTimeout.cardText2.setText(String.valueOf(new Random().nextInt(100)));
        binding.homeCardTimeout.cardText2.setTextColor(Color.RED);
        binding.homeCardTimeout.cardDetail.setOnClickListener(v -> {
            viewPager.setCurrentItem(3);
        });
    }

    public void updateReadyCount(String number) {
        TextView textView = findViewById(R.id.card_text2);
        textView.setText(number);
    }
}